package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.saas.domain.dao.ProductStockInfoDao;
import com.xbongbong.saas.domain.entity.ProductStockInfoEntity;
import com.xbongbong.saas.model.ProductStockInfoModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Jian.Wang
 * @date  2019-01-28 9:11
 **/
@Service("ProductStockInfoModel")
public class ProductStockInfoModelImpl implements ProductStockInfoModel{
    @Resource
    private ProductStockInfoDao productStockInfoDao;
    /**
     * 根据索引查询数据
     *
     * @param param 查询参数
     * @return List<ProductStockInfoEntity>
     * @author Jian.Wang
     * @date 2019/2/21 16:51
     * @since v1.0
     **/
    @Override
    public List<ProductStockInfoEntity> findEntitys(Map<String, Object> param) {
        return productStockInfoDao.findEntitys(param);
    }
    /**
     * 入库单产品数据插入
     * @param productStockInfoEntity 供应商实体
     * @author Jian.Wang
     * @date 2019/1/23 20:27
     * @since v1.0
     * @return Integer
     */
    @Override
    public Integer insert(ProductStockInfoEntity productStockInfoEntity) {
        productStockInfoEntity.setAddTime(DateTimeUtil.getInt());
        productStockInfoEntity.setUpdateTime(DateTimeUtil.getInt());
        return productStockInfoDao.insert(productStockInfoEntity);
    }
    /**
     * 入库单产品数据更新
     * @param productStockInfoEntity 供应商实体
     * @author Jian.Wang
     * @date 2019/1/23 20:27
     * @since v1.0
     * @return Integer
     */
    @Override
    public Integer update(ProductStockInfoEntity productStockInfoEntity) {
        productStockInfoEntity.setUpdateTime(DateTimeUtil.getInt());

        return productStockInfoDao.update(productStockInfoEntity);
    }
    /**
     * 入库单产品数据保存
     * @param productStockInfo 保存实体
     * @author Jian.Wang
     * @date 2019/1/23 20:27
     * @since v1.0
     * @throws XbbException 异常
     * @return Integer
     */
    @Override
    public Integer save(ProductStockInfoEntity productStockInfo) {
        if (productStockInfo.getId() == null || productStockInfo.getId().equals(0L)) {
            return insert(productStockInfo);
        }
        return update(productStockInfo);
    }

    @Override
    public List<ProductStockInfoEntity> getByRefIdAndRefType(Long refId, Integer refType, String corpid) {
        Map<String, Object> params = new HashMap<>();
        params.put("corpid", corpid);
        params.put("refId", refId);
        params.put("refType", refType);
        params.put("del", 0);
        return findEntitys(params);
    }

    @Override
    public List<ProductStockInfoEntity> getByRefIdInAndRefType(List<Long> refIdIn, Integer refType, String corpid) {
        Map<String, Object> params = new HashMap<>();
        params.put("corpid", corpid);
        params.put("refIdIn", refIdIn);
        params.put("refType", refType);
        params.put("del", 0);
        return findEntitys(params);
    }

    /**
     * 根据关联入库/出库关联产品id获取
     */
    @Override
    public List<ProductStockInfoEntity> getByRefId(Long instockProductId, String corpid, Integer refType) {
        Map<String, Object> params = new HashMap<>();
        params.put("corpid", corpid);
        params.put("refId", instockProductId);
        params.put("refType", refType);
        params.put("del", 0);
        return findEntitys(params);
    }

    @Override
    public Integer deleteByRefIdIn(String corpid, List<Long> refIdIn, Integer refType) {
        return productStockInfoDao.deleteByRefIdIn(corpid, refIdIn, refType);
    }

    @Override
    public List<Long> findGuaranteePeriodProductIds(Map<String, Object> param) {
        return productStockInfoDao.findGuaranteePeriodProductIds(param);
    }

    @Override
    public Integer deleteByIdIn(List<Long> idIn, String corpid) {
        return productStockInfoDao.deleteByIdIn(idIn,corpid);
    }
}
